package com.ymatou.iapi.godmch.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.internal.runners.TestMethod;

import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.applySupplierBean;
import com.ymatou.iapi.godmch.parameter.authShopDesignBean;
import com.ymatou.iapi.godmch.parameter.bindmchBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.service.CreateCodeCall;
import com.ymatou.iapi.godmch.service.applyAgentCall;
import com.ymatou.iapi.godmch.service.applySupplierCall;
import com.ymatou.iapi.godmch.service.authShopDesignCall;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.cs_info;
import com.ymttest.database.model.godmch.shop_design_auth;
import com.ymttest.database.model.godmch.shop_design_authExample;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.database.sqlwapper.godmch.cs_infoWapper;
import com.ymttest.database.sqlwapper.godmch.shop_design_authWapper;
import com.ymttest.utils.TestDataManager;
/**
*授权上级装修
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_authShopDesign{
	private static authShopDesignBean bean;
	private static authShopDesignCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("授权上级装修");
	}

	@Before
	public void caseUp() {
		bean=new authShopDesignBean();
		call=new authShopDesignCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	 private void check_verify( ) throws InterruptedException {
		 
	    Logger.verifyEquals("200", call.getCode(), "验证返回Code");
		DefGodMchWapper defWapper=new DefGodMchWapper();
		List<cs_info> cs_infos=defWapper.selectCsInfoByMchId(bean.getMchId());
		if(cs_infos.size()>0){
			Logger.verifyEquals(309, cs_infos.get(0).getTempldateId(), "代理美工创建成功");
		}
		else{
			Logger.verifyEquals(false, true, "代理美工创建失败");
		}
		  
		shop_design_authWapper shop_design_authWapper=new shop_design_authWapper();
		shop_design_authExample example=new shop_design_authExample();
		example.createCriteria().andMch_idEqualTo(bean.getMchId()).andParent_mch_idEqualTo(bean.getParentMchId());
		shop_design_auth shop=shop_design_authWapper.selectByExample(example).get(0);
		Logger.verifyEquals(0, shop.getAuth_state(), "验证Auth_state有效");

	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_authShopDesign_001() {
		Logger.start(true,"授权上级装修-平台商户-上下级存在-返回成功");
		try {
			Integer parentMchId=GodmchManageCallService.CreateSellerMch();
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			//渠道商1邀请码
			CreateCodeBean createbean=GodmchManageCallService.InitcreateCodeBeanForAgent(parentMchId);
			CreateCodeCall createcall=GodmchManageCallService.createCodeCall(createbean);
			//绑定关系
			bindmchBean bindmchBean=GodmchManageCallService.InitbindmchBean(mchId, createcall.getCode());
			GodmchManageCallService.bindmchcall(bindmchBean);
			
			bean.setMchId(mchId);
			bean.setParentMchId(parentMchId);
			bean.setAppKey("appKey_"+TestDataManager.getRandomNum(5));
			call.setData(bean);
			call.callService();			
			
			check_verify() ;
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_authShopDesign_002() {
		Logger.start(true,"授权上级装修-平台商户-上下级关系不存在-返回成功");
		try {
			Integer parentMchId=GodmchManageCallService.CreateSellerMch();
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			//渠道商1邀请码
			CreateCodeBean createbean=GodmchManageCallService.InitcreateCodeBeanForAgent(parentMchId);
			CreateCodeCall createcall=GodmchManageCallService.createCodeCall(createbean);
			//绑定关系
			bindmchBean bindmchBean=GodmchManageCallService.InitbindmchBean(mchId, createcall.getCode());
			GodmchManageCallService.bindmchcall(bindmchBean);
			
			bean.setMchId(mchId+11);
			bean.setParentMchId(parentMchId);
			bean.setAppKey("appKey_"+TestDataManager.getRandomNum(5));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("500", call.getCode(), "验证返回Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_authShopDesign_003() {
		Logger.start(true,"授权上级装修-独立商户-上下级存在-返回成功");
		try {
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
			applyAgentCall applyAgentCall=GodmchManageCallService.applyAndapproveAgent(createIndepMchCall.getmchId());
			
			bean.setMchId(applyAgentCall.getAgentId());
			bean.setParentMchId(createIndepMchCall.getmchId());
			bean.setAppKey("appKey_"+TestDataManager.getRandomNum(5));
			call.setData(bean);
			call.callService();	
			
			check_verify() ;
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_authShopDesign_003_1() {
		Logger.start(true,"授权上级装修-独立商户-上下级不存在-返回失败");
		try {
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
			applyAgentCall applyAgentCall=GodmchManageCallService.applyAndapproveAgent(createIndepMchCall.getmchId());
			
			bean.setMchId(applyAgentCall.getAgentId()+111);
			bean.setParentMchId(createIndepMchCall.getmchId());
			bean.setAppKey("appKey_"+TestDataManager.getRandomNum(5));
			call.setData(bean); 
			call.callService();	
			
			Logger.verifyEquals("500", call.getCode(), "验证返回Code");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_authShopDesign_004() {
		Logger.start(true,"授权上级装修-独立商户-供货商-返回成功");
		try {
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
			//申请供货商
			applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
			applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
			//审核
			GodmchManageCallService.approveSupplier(createIndepMchCall.getmchId(), applySupplierCall.getSupplierId());
			
			bean.setMchId(createIndepMchCall.getmchId());
			bean.setParentMchId(applySupplierCall.getSupplierId());
			bean.setAppKey("appKey_"+TestDataManager.getRandomNum(5));
			call.setData(bean); 
			call.callService();				
			
			check_verify() ;
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_authShopDesign_007() {
		Logger.start(true,"授权上级装修-商户id为空-返回失败");
		try {
			
			Integer parentMchId=GodmchManageCallService.CreateSellerMch();
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			//渠道商1邀请码
			CreateCodeBean createbean=GodmchManageCallService.InitcreateCodeBeanForAgent(parentMchId);
			CreateCodeCall createcall=GodmchManageCallService.createCodeCall(createbean);
			//绑定关系
			bindmchBean bindmchBean=GodmchManageCallService.InitbindmchBean(mchId, createcall.getCode());
			GodmchManageCallService.bindmchcall(bindmchBean);
			
			//bean.setMchId(mchId);
			bean.setParentMchId(parentMchId);
			bean.setAppKey("appKey_"+TestDataManager.getRandomNum(5));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getCode(), "验证返回Code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_authShopDesign_008() {
		Logger.start(true,"授权上级装修-上级商户id为空-返回失败");
		try {
			
				Integer parentMchId=GodmchManageCallService.CreateSellerMch();
				Integer mchId=GodmchManageCallService.CreateSellerMch();
				//渠道商1邀请码
				CreateCodeBean createbean=GodmchManageCallService.InitcreateCodeBeanForAgent(parentMchId);
				CreateCodeCall createcall=GodmchManageCallService.createCodeCall(createbean);
				//绑定关系
				bindmchBean bindmchBean=GodmchManageCallService.InitbindmchBean(mchId, createcall.getCode());
				GodmchManageCallService.bindmchcall(bindmchBean);
				
				bean.setMchId(mchId);
				//bean.setParentMchId(parentMchId);
				bean.setAppKey("appKey_"+TestDataManager.getRandomNum(5));
				call.setData(bean);
				call.callService();			
				
				Logger.verifyEquals("4001", call.getCode(), "验证返回Code");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}